package com.pengshun.modules.aliyun;

import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.pengshun.config.oss.OSSClientConstants;
import com.pengshun.modules.api.coal.util.CommonUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 阿里云图片上传
 */
public class OosUpload {

    public static String upload(InputStream in, String fileName,String path) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = OSSClientConstants.ENDPOINT;
         // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = OSSClientConstants.ACCESS_KEY_ID;
        String accessKeySecret = OSSClientConstants.ACCESS_KEY_SECRET;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        //4 设置知道文件夹
        ossClient.putObject(OSSClientConstants.BACKET_NAME, path+"/"+ fileName, in);
//        ossClient.putObject(OSSClientConstants.BACKET_NAME, fileName, in, meta);

        // 关闭OSSClient。
        ossClient.shutdown();
        String uploadUrl = "https://binmeitong.oss-cn-beijing.aliyuncs.com/" +path+"/"+ fileName;
//        String uploadUrl = "https://binmeitong.oss-cn-beijing.aliyuncs.com/"+ fileName;
        return uploadUrl;

    }
//    public static String uploadVido( MultipartFile file) throws Exception {
//
//
//        // 创建OSSClient实例。
//        OSS ossClient = new OSSClient(OSSClientConstants.ENDPOINT, OSSClientConstants.ACCESS_KEY_ID,  OSSClientConstants.ACCESS_KEY_SECRET);
//
//        String originalFilename = file.getOriginalFilename();
//        String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
//        String name = CommonUtil.getFourRandom() + substring;
//        try {
//            InputStream stream = file.getInputStream();
//            String filename = System.currentTimeMillis() + file.getOriginalFilename();
//            PutObjectResult result = ossClient.putObject(OSSClientConstants.BACKET_NAME, "video/" + name, stream);
//            client.shutdown();
//        } catch (Exception e) {
//            throw new IOException("上传失败");
//        }
//        return "https://binmeitong.oss-cn-beijing.aliyuncs.com/" + "video/" + name;
//        ObjectMetadata meta = new ObjectMetadata();
//        meta.setContentType("image/jpg");
//        //4 设置知道文件夹
//        ossClient.putObject(OSSClientConstants.BACKET_NAME, path+"/"+ fileName, in, meta);
////        ossClient.putObject(OSSClientConstants.BACKET_NAME, fileName, in, meta);
//
//        // 关闭OSSClient。
//        ossClient.shutdown();
//        String uploadUrl = " +path+"/"+ fileName;
////        String uploadUrl = "https://binmeitong.oss-cn-beijing.aliyuncs.com/"+ fileName;
//        return uploadUrl;
//
//    }



}
